Python获取IP地址的三种方法

您所在的位置:网站首页 python ip地址查询 Python获取IP地址的三种方法

Python获取IP地址的三种方法

2022-05-23 01:40| 来源: 网络整理| 查看: 265

#!/usr/bin/env python

import re,urllib2

class Get_public_ip:

    def getip(self):

        try:

            myip = self.visit("http://www.111cn.net/")

        except:

            try:

                myip = self.visit("http://www.ip138.com/ip2city.asp")

            except:

                myip = "So sorry!!!"

        return myip

    def visit(self,url):

        opener = urllib2.urlopen(url)

        if url == opener.geturl():

            str = opener.read()

        return re.search('d+.d+.d+.d+',str).group(0)

if __name__ == "__main__":

    getmyip = Get_public_ip()

    print getmyip.getip()


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3